package com.zsyh.jiujiu.Adress;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.DizhiBean;
import com.zsyh.jiujiu.net.MyObserver;

import java.util.ArrayList;
import java.util.List;

import chihane.jdaddressselector.model.Province;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by zzq on 16/9/26.
 */
public class ProvinceFragment implements AdapterView.OnItemClickListener{
    private AddressCallBack callBack;
    private String code;
    private Activity context;
    private AddressAdapter adapter;
    private ListView listview;
    private  List<AddressManager.Province> list=new ArrayList<>();



    public ProvinceFragment(Activity context, AddressCallBack callBack){
        this.context = context;
        this.callBack = callBack;
        initView();
    }
    public void setCode(String code){
        this.code = code;

    }

    public ListView getListview() {
        return listview;
    }

    public View initView() {
        listview = (ListView) LayoutInflater.from(context).inflate(R.layout.select_address_pop_listview,null);
        sheng("");
        listview.setOnItemClickListener(this);
        return listview;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //code = AddressManager.newInstance().getAllProvinces().get(i).getCode();
        code = list.get(i).getCode();
        Log.e("codef",code);
        if (callBack != null){
            callBack.selectProvince(list.get(i));
        }
        adapter.notifyDataSetChanged();
    }

    class AddressAdapter extends BaseAdapter {

        private List<AddressManager.Province> list;

        public AddressAdapter(List<AddressManager.Province> list){
            this.list = list;
        }

        @Override
        public int getCount() {
            if (list != null) {
                return list.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(context).inflate(R.layout.address_listiew_item_textview, null);
            TextView text = (TextView) view.findViewById(R.id.tvTextName);
            ImageView ivSelect = (ImageView) view.findViewById(R.id.ivSelect);
            text.setText(list.get(i).getName());
            Log.e("shenfen",list.get(0).getCode());
            if (list.get(i).getName().equals(code)) {
                text.setTextColor(context.getResources().getColor(R.color.text_button));
                ivSelect.setVisibility(View.VISIBLE);
            }
            return view;
        }
    }


    public void sheng(String id){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getAreaByPid(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(context,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("dizhi",JsonStr);
                        Gson gson=new Gson();
                        DizhiBean dizhiBean=gson.fromJson(JsonStr,DizhiBean.class);

                        for(int i=0;i<dizhiBean.getData().size();i++){
                            AddressManager.Province province = new AddressManager.Province(dizhiBean.getData().get(i).getAreaName(), dizhiBean.getData().get(i).getId());
                            list.add(province);
                        }

                        adapter=new AddressAdapter(list);
                        listview.setAdapter(adapter);


                    }
                });
    }


}
